package com.beimi.core.engine.game.action.dizhu;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import com.beimi.core.BMDataContext;
import com.beimi.core.engine.game.task.majiang.CreateMJPlayCardsTask;
import com.beimi.core.statemachine.action.Action;
import com.beimi.core.statemachine.impl.BeiMiExtentionTransitionConfigurer;
import com.beimi.core.statemachine.message.Message;
import com.beimi.util.cache.CacheHelper;
import com.beimi.util.rules.model.Board;
import com.beimi.web.model.GameRoom;

/**
 * 凑够了，开牌
 *
 * @author iceworld
 */
@Slf4j
public class PlayMJCardsAction<T, S> implements Action<T, S> {
    @Override
    public void execute(Message<T> message, BeiMiExtentionTransitionConfigurer<T, S> configurer) {
        log.info("method: {},data:{}", "PlayMJCardsAction.execute", JSON.toJSONString(message));

        String room = (String) message.getMessageHeaders().getHeaders().get("room");
        if (!StringUtils.isBlank(room)) {
            GameRoom gameRoom = (GameRoom) CacheHelper.getGameRoomCacheBean().getCacheObject(room, BMDataContext.SYSTEM_ORGI);
            if (gameRoom != null) {
                Board board = (Board) CacheHelper.getBoardCacheBean().getCacheObject(gameRoom.getId(), gameRoom.getOrgi());
                int interval = (int) message.getMessageHeaders().getHeaders().get("interval");
                String nextPlayer = board.getBanker();
                if (!StringUtils.isBlank(board.getNextplayer().getNextplayer())) {
                    nextPlayer = board.getNextplayer().getNextplayer();
                }
                //加入自动出牌定时器
                CacheHelper.getExpireCache().put(gameRoom.getRoomid(), new CreateMJPlayCardsTask(interval, nextPlayer, gameRoom, gameRoom.getOrgi()));
            }
        }
    }
}
